//
//  QDRecordDetailViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/1.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 扣费详情
final class QDRecordDetailViewController: QDLargeRefreshTitleViewController {
    
    /// 类型，默认为地铁
    var type = RecordType.metro
    /// 模型
    var traffic = QDTrafficModel()
    
    
    override func loadPropertysAtInitialization() {
        super.loadPropertysAtInitialization()
        
        headerRefreshEnable = false
        footerRefreshEnable = false
    }
    
    
    override func registerTableViewCellsAndReusableView() {
        
        tableView.register(QDRecordBusTableCell.self, forCellReuseIdentifier: RecordType.bus.rawValue)
        tableView.register(QDRecordMetroTableCell.self, forCellReuseIdentifier: RecordType.metro.rawValue)
        tableView.register(QDOrderInfoTableViewCell.self, forCellReuseIdentifier: "info")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        largeTableHeaderView.backgroundColor = RecordBackgroundColor
        largeTableHeaderView.title = "扣费详情"
    }
    
    
    // MARK: NavigationBar
    override var nav_mutableBarShadowHidden: Bool { return true }
    override var nav_barTintColor: UIColor { return RecordBackgroundColor }
    /// 背景色
    override var tableViewBackgroundColor: UIColor { return RecordBackgroundColor }

}


extension QDRecordDetailViewController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: indexPath.section == 0 ? type.rawValue : "info", for: indexPath)
        
        if let metroCell = cell as? QDRecordMetroTableCell { // 地铁
            metroCell.updateByMetro(traffic: traffic)
        }
        
        else if let busCell = cell as? QDRecordBusTableCell { // 公交
            busCell.updateByBus(traffic: traffic)
        }
        
        else if let infoCell = cell as? QDOrderInfoTableViewCell {//信息
            
            infoCell.subtitles = traffic.trafficInfos(type: type)
            infoCell.reload()
        }

        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return indexPath.section == 0 ? type.rowHeight : QDOrderInfoTableViewCell.height(count: 4)
    }
    
    
    /// 占位headerView
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = RecordBackgroundColor
        return view
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 10 : 6
    }
    
}

private extension QDTrafficModel {
    
    /// 进行返回
    func trafficInfos(type:RecordType = .metro) -> [String]{
        
        if type == .metro {
            return [tradeOrderNo, //订单编号
                    (QDPayType(rawValue: payChannelCode) ?? .unknown ).payTypeTitle,//支付方式
                    metroPayTime,//支付时间
                    "\(NSNumber(value: (Double(payAmount) ?? 0.0) / 100.0))元"]//支付总价
        }
        
        return [sysSeq, //交易编号
                (QDPayType(rawValue: paymentChannel) ?? .unknown ).payTypeTitle, //支付方式
                busPayTime,//支付时间
                "\(NSNumber(value: (Double(origAmt) ?? 0.0) / 100.0))元"]
    }
}

